﻿using BZ.Logger;
using HalconDotNet;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.Monitor;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;

namespace NDK.Module.StationLogic
{
    public class Station_转盘二穴 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_转盘二穴 Instance = new Station_转盘二穴();
        public Stopwatch SunWay02 = new Stopwatch();
        public string StationName = "Station02转盘2工位";


        //步骤字段、属性
        private StationStep _Step = StationStep.判断是否工作;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if(_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            StationID = 2;
            CurHoleId = 8;
            logBaseInfo = "转盘二穴流程";
            Step = StationStep.判断是否工作;
            FlowCT = new Stopwatch();
            MVCCD = Common.HikVisionList[2];
            QueueSN = new ConcurrentQueue<string>();
            QueueImage = new ConcurrentQueue<MeasureImage>();

            VisionFlowName = $"上CCD_{Common.CurProType}";
        }

        public override void ActionProcess ()
        {
            try
            {
                switch(Step)
                {
                    case StationStep.判断是否工作:
                        if(!Common.AllStationCanWork)
                            break;
                        if(!Common.DicStationWorkStatus[StationID])
                        {
                            Step = Common.DicHoleCanWork[CurHoleId] ? StationStep.触发相机拍照 : StationStep.工位流程完成;
                        }
                        SunWay02.Restart();
                        break;

                    case StationStep.触发相机拍照:
                        Machine.imageAllLight[CurHoleId - 1].Open();
                        Thread.Sleep(Common.IP_光源延时);
                        if(Common.IsEmptyModel)
                        {
                            Thread.Sleep(1000);
                            Step = StationStep.工位流程完成;
                        }
                        else
                        {
                            if(CCDImage(ref HImageCCD))
                            {
                                string DefineSn = DateTime.Now.ToString("yyyyMMddHHmmssffff");
                                QueueSN.Enqueue(DefineSn);
                                var images = new (string, HImage)[] { ("上CCD", HImageCCD.Clone()) };
                                QueueImage.Enqueue(new MeasureImage() { Hole = CurHoleId, Images = images });
                                DataProcess.Instance.VisionProcessAsync($"上CCD_{Common.CurProType}", $"上CCD_{Common.CurProType}Data", CurHoleId, DefineSn, images);
                                Step = StationStep.工位流程完成;
                            }
                        }
                        Machine.imageAllLight[CurHoleId - 1].Close();
                        break;

                    case StationStep.工位流程完成:
                        CurHoleId++;
                        if(CurHoleId > 8)
                        {
                            CurHoleId = 1;
                        }
                        Common.ChangeTurnTableStatus(StationID, true);
                        Step = StationStep.判断是否工作;
                        break;

                    default:
                        break;
                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();
        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            判断是否工作,
            触发相机拍照,
            工位流程完成
        }
    }

}
